package jp.sinya;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        System.out.println("get...");
        ServletContext servletContext = this.getServletContext();
        Integer count = (Integer) servletContext.getAttribute("count");
        if (count == null) {
            servletContext.setAttribute("count", 1);
        } else {
            servletContext.setAttribute("count", count + 1);
        }

        PrintWriter pw = resp.getWriter();
        pw.print("<h3>access counts : " + count + "</h3>");
    }
}
